﻿package org.xhs.report.doc.ui
{
    import __AS3__.vec.*;
    import baidu.ui.events.*;
    import baidu.ui.managers.*;
    import baidu.wenku.common.*;
    import baidu.wenku.common.model.*;
    import baidu.wenku.common.ui.*;
    import baidu.wenku.common.ui.panel.*;
    import baidu.wenku.doc.*;
    import baidu.wenku.doc.model.*;
    import flash.display.*;
    import flash.events.*;

    public class DocViewerBase extends Sprite implements IDocViewer
    {
        protected var _pageth:int;
        protected var _zoomNumber:Number = 1;
        protected var container:Sprite;
        protected var scrollPane:WenkuScrollPane;
        protected var _pagesArray:Vector.<BlankPage>;
        protected var _positionX:Number = 0;
        protected var _positionY:Number = 0;
        protected var _mouseDown:Boolean = false;
        protected var _inDocView:Boolean = false;
        protected var loaderInfoVo:LoaderInfoVO;
        protected var screenModel:ScreenModel;
        protected var storeModel:StoreModel;
        protected var storePage:StorePage;
        protected var docLocatModel:DocLocatModel;
        protected var zoomModel:DocZoomModel;

        public function DocViewerBase()
        {
            this.init();
            return;
        }// end function

        protected function init() : void
        {
            this.loaderInfoVo = InstanceManager.createSingletonInstance(LoaderInfoVO);
            this.zoomModel = InstanceManager.createSingletonInstance(DocZoomModel);
            this.storeModel = InstanceManager.createSingletonInstance(StoreModel);
            this.storePage = InstanceManager.createSingletonInstance(StorePage);
            this.screenModel = InstanceManager.createSingletonInstance(ScreenModel);
            this.docLocatModel = InstanceManager.createSingletonInstance(DocLocatModel);
            return;
        }// end function

        protected function initViewer() : void
        {
            if (!this.scrollPane)
            {
                this.scrollPane = new WenkuScrollPane();
                this.scrollPane.visible = false;
                this.scrollPane.vScrollBar.setStyle("skin", ScrollBar_Skin);
                this.scrollPane.hScrollBar.setStyle("skin", ScrollBar_Skin);
                this.scrollPane.setStyle("skin", ScrollPane_Skin);
                this.scrollPane.vScrollBar.addEventListener(ScrollEvent.SCROLL, this.scrollPosChange);
            }
            if (!this.container)
            {
                this.container = new Sprite();
                this.container.addEventListener(MouseEvent.ROLL_OVER, this.docMouseOver);
                this.container.addEventListener(MouseEvent.ROLL_OUT, this.docMouseOut);
                this.container.addEventListener(MouseEvent.MOUSE_DOWN, this.docMouseDown);
                this.container.addEventListener(MouseEvent.MOUSE_MOVE, this.docGlobalMouseMove);
            }
            while (this.container.numChildren > 0)
            {
                
                this.container.removeChildAt(0);
            }
            this.scrollPane.content = this.container;
            this.scrollPane.setSize(this.screenModel.stageWidth - 2, this.screenModel.stageHeight - Config.SB_HEIGHT - Config.TB_HEIGHT);
            addChild(this.scrollPane);
            this.docLocatModel.addEventListener(LocatEvent.PAGETH_CHANGED_SCROLL, this.showPage);
            this.docLocatModel.addEventListener(LocatEvent.PAGE_REARRANGED, this.setPagesPos);
            this.docLocatModel.addEventListener(LocatEvent.FULLSCREEN_SWITCHING, this.fullscreenSwitching);
            this.screenModel.addEventListener(Event.RESIZE, this.screenResize);
            return;
        }// end function

        protected function screenResize(event:Event = null) : void
        {
            this.reset();
            return;
        }// end function

        protected function reset() : void
        {
            this.scrollPane.setSize(this.screenModel.stageWidth - 2, this.screenModel.stageHeight - Config.SB_HEIGHT - Config.TB_HEIGHT);
            this.setOptimalZoom();
            return;
        }// end function

        protected function showPage(event:Event = null) : void
        {
            return;
        }// end function

        protected function setPagesPos(event:LocatEvent = null) : void
        {
            return;
        }// end function

        protected function fullscreenSwitching(event:LocatEvent) : void
        {
            if (this.docLocatModel.inSwitching)
            {
                CursorManager.getInstance().hideCurrentCursor();
                if (this.container && this.container.hasEventListener(MouseEvent.ROLL_OVER))
                {
                    this.container.removeEventListener(MouseEvent.ROLL_OVER, this.docMouseOver);
                }
            }
            else
            {
                CursorManager.getInstance().hideCurrentCursor();
                if (this.container && !this.container.hasEventListener(MouseEvent.ROLL_OVER))
                {
                    this.container.addEventListener(MouseEvent.ROLL_OVER, this.docMouseOver);
                }
            }
            return;
        }// end function

        protected function resetContainerPosition() : void
        {
            this.scrollPane.update();
            if (this.scrollPane.maxHScrollPosition > 0)
            {
                this.scrollPane.content.y = Math.max(0, (this.screenModel.stageHeight - Config.SB_HEIGHT - Config.TB_HEIGHT - Config.DV_WIDTH_RIGHT_SCROLLBAR - this.scrollPane.content.height) / 2);
            }
            else
            {
                this.scrollPane.content.y = Math.max(0, (this.screenModel.stageHeight - Config.SB_HEIGHT - Config.TB_HEIGHT - this.scrollPane.content.height) / 2);
            }
            if (this.scrollPane.maxVScrollPosition > 0)
            {
                this.scrollPane.content.x = Math.max(DocConfig.DV_WIDTH_LEFT_PADDING, (this.screenModel.stageWidth - Config.DV_WIDTH_RIGHT_SCROLLBAR - this.scrollPane.content.width) / 2);
            }
            else
            {
                this.scrollPane.content.x = Math.max(DocConfig.DV_WIDTH_LEFT_PADDING, (this.screenModel.stageWidth - this.scrollPane.content.width) / 2);
            }
            this.scrollPane.update();
            return;
        }// end function

        protected function scrollPosChange(event:ScrollEvent) : void
        {
            return;
        }// end function

        protected function docMouseOver(event:MouseEvent = null) : void
        {
            this._inDocView = true;
            if (!event.buttonDown && !this.docLocatModel.inSwitching)
            {
                CursorManager.getInstance().showCursor(ReadCursor);
            }
            return;
        }// end function

        protected function docMouseOut(event:MouseEvent = null) : void
        {
            this._inDocView = false;
            if (!event.buttonDown && !this.docLocatModel.inSwitching)
            {
                CursorManager.getInstance().hideCurrentCursor();
            }
            return;
        }// end function

        protected function docMouseDown(event:MouseEvent = null) : void
        {
            this._mouseDown = true;
            this._positionX = this.screenModel.stage.mouseX;
            this._positionY = this.screenModel.stage.mouseY;
            this.screenModel.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.docMouseMove);
            this.screenModel.stage.addEventListener(MouseEvent.MOUSE_UP, this.docMouseUp);
            if (!this.docLocatModel.inSwitching)
            {
                CursorManager.getInstance().showCursor(DragCursor);
            }
            if (!this.container.hasEventListener(MouseEvent.MOUSE_UP))
            {
                this.container.addEventListener(MouseEvent.MOUSE_UP, this.docMouseClick);
            }
            return;
        }// end function

        protected function docMouseUp(event:MouseEvent = null) : void
        {
            this._mouseDown = false;
            if (!this.docLocatModel.inSwitching)
            {
                CursorManager.getInstance().showCursor(ReadCursor);
            }
            this.docGlobalMouseMove();
            this._positionX = 0;
            this._positionY = 0;
            this.screenModel.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.docMouseMove);
            this.screenModel.stage.removeEventListener(MouseEvent.MOUSE_UP, this.docMouseUp);
            return;
        }// end function

        protected function docMouseMove(event:MouseEvent = null) : void
        {
            var _loc_2:* = this.screenModel.stage.mouseX - this._positionX;
            var _loc_3:* = this.screenModel.stage.mouseY - this._positionY;
            this._positionX = this.screenModel.stage.mouseX;
            this._positionY = this.screenModel.stage.mouseY;
            this.scrollPane.hScrollPosition = this.scrollPane.hScrollPosition - _loc_2;
            this.scrollPane.vScrollPosition = this.scrollPane.vScrollPosition - _loc_3;
            if (this.container.hasEventListener(MouseEvent.MOUSE_UP))
            {
                this.container.removeEventListener(MouseEvent.MOUSE_UP, this.docMouseClick);
            }
            return;
        }// end function

        protected function docGlobalMouseMove(event:MouseEvent = null) : void
        {
            return;
        }// end function

        protected function docMouseClick(event:MouseEvent = null) : void
        {
            this.container.removeEventListener(MouseEvent.MOUSE_UP, this.docMouseClick);
            return;
        }// end function

        public function setOptimalZoom() : void
        {
            return;
        }// end function

        public function destroy() : void
        {
            this.docLocatModel.removeEventListener(LocatEvent.PAGETH_CHANGED_SCROLL, this.showPage);
            this.docLocatModel.removeEventListener(LocatEvent.PAGE_REARRANGED, this.setPagesPos);
            this.screenModel.removeEventListener(Event.RESIZE, this.screenResize);
            if (this.scrollPane)
            {
                if (this.scrollPane.parent)
                {
                    this.scrollPane.parent.removeChild(this.scrollPane);
                }
                this.scrollPane.vScrollBar.removeEventListener(ScrollEvent.SCROLL, this.scrollPosChange);
                this.scrollPane = null;
            }
            if (this.container)
            {
                while (this.container.numChildren)
                {
                    
                    this.container.removeChildAt(0);
                }
                this.container.removeEventListener(MouseEvent.ROLL_OVER, this.docMouseOver);
                this.container.removeEventListener(MouseEvent.ROLL_OUT, this.docMouseOut);
                this.container.removeEventListener(MouseEvent.MOUSE_DOWN, this.docMouseDown);
                this.container.removeEventListener(MouseEvent.MOUSE_UP, this.docMouseClick);
                this.container.removeEventListener(MouseEvent.MOUSE_MOVE, this.docGlobalMouseMove);
                this.container = null;
            }
            return;
        }// end function

        public function set data(param1:Vector.<BlankPage>) : void
        {
            this._pagesArray = param1;
            this.initViewer();
            this.setPagesPos();
            this.showPage();
            this.scrollPane.visible = true;
            return;
        }// end function

        public function set zoomNumber(param1:Number) : void
        {
            return;
        }// end function

        public function get zoomNumber() : Number
        {
            return this._zoomNumber;
        }// end function

        public function get verticalScrollVisible() : Boolean
        {
            return this.scrollPane.maxVScrollPosition > 0;
        }// end function

        public function get horizontalScrollVisible() : Boolean
        {
            return this.scrollPane.maxHScrollPosition > 0;
        }// end function

        public function get vScrollPosition() : Number
        {
            return this.scrollPane.vScrollPosition;
        }// end function

        public function set vScrollPosition(param1:Number) : void
        {
            this.scrollPane.vScrollPosition = param1;
            return;
        }// end function

        public function get maxVScrollPosition() : Number
        {
            return this.scrollPane.maxVScrollPosition;
        }// end function

        public function get vScrollbarHeight() : Number
        {
            return this.scrollPane.vScrollBar.height;
        }// end function

        public function get contentWidth() : Number
        {
            return this.container.width;
        }// end function

    }
}
